package net.arvin.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.arvin.mobilebanksystem.R;
import net.arvin.model.Bills;
import net.arvin.model.Card;
import net.arvin.model.Time;
import net.arvin.model.User;
import net.arvin.utils.TransfersType;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class StickListTaskAdapter extends BaseAdapter implements
		SectionIndexer, StickyListHeadersAdapter {

	private LayoutInflater mInflater;
	private List<Bills> bills;
	private int[] sectionIndices;
	private String[] sectionHeaders;

	public StickListTaskAdapter(Context context, List<Bills> bills) {
		this.mInflater = LayoutInflater.from(context);
		this.bills = bills;
		sectionIndices = getSectionIndices();
		sectionHeaders = getSectionHeaders();
	}

	public void refresh(List<Bills> bills) {
		this.bills = bills;
		sectionIndices = getSectionIndices();
		sectionHeaders = getSectionHeaders();
		notifyDataSetChanged();
	}

	private int[] getSectionIndices() {
		List<Integer> sectionIndices = new ArrayList<Integer>();
		String lastCreateDate = new Time().getMonth(bills.get(0).getTime());
		sectionIndices.add(0);
		for (int i = 1; i < bills.size(); i++) {
			String createDate = new Time().getMonth(bills.get(i).getTime());
			if (!createDate.equals(lastCreateDate)) {
				lastCreateDate = createDate;
				sectionIndices.add(i);
			}
		}
		int[] sections = new int[sectionIndices.size()];
		for (int i = 0; i < sectionIndices.size(); i++) {
			sections[i] = sectionIndices.get(i);
		}
		return sections;
	}

	private String[] getSectionHeaders() {
		String[] sectionHeaders = new String[sectionIndices.length];
		for (int i = 0; i < sectionIndices.length; i++) {
			sectionHeaders[i] = new Time().getMonth(bills
					.get(sectionIndices[i]).getTime());
		}
		return sectionHeaders;
	}

	@Override
	public int getCount() {
		return bills.size();
	}

	@Override
	public Object getItem(int position) {
		return bills.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listitem_bills, null);
			holder = initView(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		setData(position, holder);
		return convertView;
	}

	private ViewHolder initView(View convertView) {
		ViewHolder holder;
		holder = new ViewHolder();
		holder.icon = (ImageView) convertView.findViewById(R.id.bills_icon);
		holder.name = (TextView) convertView.findViewById(R.id.bills_name);
		holder.time = (TextView) convertView.findViewById(R.id.bills_time);
		holder.money = (TextView) convertView.findViewById(R.id.bills_money);
		return holder;
	}

	private void setData(int position, ViewHolder holder) {
		holder.icon.setImageResource(R.drawable.avatar);
		setName(position, holder);
		setTime(position, holder);
		setMoney(position, holder);
	}

	private void setMoney(int position, ViewHolder holder) {
		if (bills.get(position).getType().equals("话费充值")) {
			holder.money.setText("-" + bills.get(position).getMoney());
		} else {
			holder.money.setText(bills.get(position).getMoney());
		}
	}

	private void setTime(int position, ViewHolder holder) {
		Time time = new Time();
		time.toTime(bills.get(position).getTime());
		if (time.year == Calendar.getInstance().get(Calendar.YEAR)) {
			holder.time.setText(time.month + "-" + time.day);
		} else {
			holder.time.setText(time.year + "-" + time.month + "-" + time.day);
		}
	}

	private void setName(int position, ViewHolder holder) {
		User user = bills.get(position).getUser();
		Card card = bills.get(position).getCard();
		if (bills.get(position).getType().equals(TransfersType.U2U)) {
			holder.name.setText(user.getNickname() + "-转账");
		} else if (bills.get(position).getType().equals(TransfersType.C2U)
				|| bills.get(position).getType().equals(TransfersType.U2C)) {
			holder.name.setText(card.getCardbank()
					+ "("
					+ card.getCardnumber().substring(
							card.getCardnumber().length() - 3) + ")");
		} else {
			holder.name.setText("充值" + getMoney(bills.get(position).getMoney())
					+ "元-" + user.getPhonenumber());
		}
	}

	private String getMoney(String price) {
		String money = null;
		if (Float.valueOf(price) <= 30) {
			return price;
		}
		if (price.equals(49.95 + "")) {
			money = 50 + "";
		}
		if (price.equals(99.90 + "")) {
			money = 100 + "";
		}
		if (price.equals(199.60 + "")) {
			money = 200 + "";
		}
		if (price.equals(299.50 + "")) {
			money = 300 + "";
		}
		if (price.equals(499.00 + "")) {
			money = 500 + "";
		}
		return money;
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		TextView headerTv = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listitem_header, null);
			headerTv = (TextView) convertView.findViewById(R.id.bills_header);
			convertView.setTag(headerTv);
		} else {
			headerTv = (TextView) convertView.getTag();
		}
		String month = new Time().getMonth(bills.get(position).getTime());
		if (month.equals(String.valueOf(Calendar.getInstance().get(
				Calendar.YEAR)))) {
			month = "本";
		}
		headerTv.setText(month);
		return convertView;
	}

	@Override
	public long getHeaderId(int position) {
		return Long.valueOf(new Time().getMonth(bills.get(position).getTime()));
	}

	@Override
	public Object[] getSections() {
		return sectionHeaders;
	}

	@Override
	public int getPositionForSection(int section) {
		if (section >= sectionIndices.length) {
			section = sectionIndices.length - 1;
		} else if (section < 0) {
			section = 0;
		}
		return sectionIndices[section];
	}

	@Override
	public int getSectionForPosition(int position) {
		for (int i = 0; i < sectionIndices.length; i++) {
			if (position < sectionIndices[i]) {
				return i - 1;
			}
		}
		return sectionIndices.length - 1;
	}

	public class ViewHolder {
		public ImageView icon;
		public TextView name;
		public TextView time;
		public TextView money;
	}
}
